// myself/enroll/enroll.js
import api from '../../utils/api.js';
import network from '../../utils/network.js';
import utils from '../../utils/utils.js';
import notice from '../../utils/notice.js';

const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        style: app.globalData.style,
        active: {
          images:["http://devhui.salonhui.com/assets/cover2.png"],
          name: '“瑜”你成长 ‘伽’倍幸福瑜伽透过每一次柔软的呼吸身体和精神的纽带在体式中重新联结我们的肢愈加舒展心灵也愈加平和是意想不到的绝佳尝试',
          bookingTimes:[
            {
              selected: true,
              format: '2024年2月12日  09:00~12:00 (上午)'
            },
            {
              selected: true,
              format: '2024年2月13日 14:00~17:00 (下午)'
            },
            {
              selected: true,
              format: '2024年2月13日 19:00~22:00 (晚上)'
            }
          ]
        },
        userInfo: {},
        name: '',
        mobile: '',
        total: 0,
        bookingTimes: [
          {},
          {},
          {}
        ]
        // visible: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let active = JSON.parse(decodeURIComponent(options.active));
        let bookingTimes = [];
        for (let i = 0; i < active.bookingTimes.length; i++) {
            console.log(active.bookingTimes[i].selected)
            if (active.bookingTimes[i].selected) {
                bookingTimes.push(active.bookingTimes[i]);
            }
        }
        // network.getJson(api.activePartTimes, {
        //     userId: app.globalData.userInfo.id,
        //     activeId: active.id
        // }).then(res => {
        //     let timers = res.data;
        //     for (let i = 0; i < active.bookingTimes.length; i++) {
        //         if (timers.indexOf(active.bookingTimes[i].id) != -1) {
        //             active.bookingTimes[i].isEnroll = 1;
        //         } else {
        //             active.bookingTimes[i].isEnroll = 0;
        //         }
        //         active.bookingTimes[i].selected = false;
        //         this.setData({
        //             active
        //         })
        //     }
        // })

        console.log(active)
        this.setData({
            userInfo: app.globalData.userInfo,
            name: app.globalData.userInfo.name,
            mobile: app.globalData.userInfo.mobile,
            active,
            bookingTimes
        })
        this.computeCount();
        notice.addNotice("UserInfo", this.refreshUserInfo, this);
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        notice.removeNotice("UserInfo", this);
    },

    refreshUserInfo() {
        let userInfo = app.globalData.userInfo;
        this.setData({
            name: userInfo.name,
            mobile: userInfo.mobile
        })
    },

    getUserInfo() {
        wx.getUserProfile({
            desc: '用于完善用户资料',
            success: (res) => {
                this.setData({
                    name: res.userInfo.nickName
                })
            }
        })
    },

    nameInput(e) {
        this.data.name = e.detail.value;
    },

    mobileInput(e) {
        this.data.mobile = e.detail.value;
    },

    goInfo() {
        wx.navigateTo({
            url: '/myself/user/info',
        })
    },

    codeInput(e) {
        this.data.code = e.detail.value;
    },

    getRealtimePhoneNumber(e) {
        network.getJson(api.getUserPhoneNumber, {
            userId: app.globalData.userInfo.id,
            code: e.detail.code
        }).then(res => {
            this.setData({
                mobile: res.data
            })
        })
    },

    selectedTap(e) {
        let index = e.currentTarget.dataset.idx;
        this.selected(index, true);
    },

    cancelTap(e) {
        let index = e.currentTarget.dataset.idx;
        this.selected(index, false);
    },

    selected(index, value) {
        this.setData({
            [`active.bookingTimes[${index}].selected`]: value
        })
        notice.postNotice("RefreshActiveTimer", this.data.active);
        this.computeCount();
    },
    computeCount() {
        let total = 0;
        for (let i = 0; i < this.data.active.bookingTimes.length; i++) {
            if (this.data.active.bookingTimes[i].selected) {
                total += this.data.active.bookingTimes[i].amount;
            }
        }
        this.setData({
            total: total.toFixed(2)
        })
    },

    onSubmit(e) {
        wx.reLaunch({
          url: '/home/success/success?mode=ENROLL&activeId=' + this.data.active.id,
        })
        // this.name = e.detail.value.name;
        // utils.fromEvent().then(() => {
        //     if (this.data.active.mode == 1) {
        //         if (this.data.code != this.data.active.code) {
        //             wx.showToast({
        //                 icon: 'none',
        //                 title: '请输入正确的邀请码'
        //             })
        //             return;
        //         }
        //     }
        //     let arr = [];
        //     for (let i = 0; i < this.data.active.bookingTimes.length; i++) {
        //         if (this.data.active.bookingTimes[i].selected) {
        //             let param = {
        //                 activeId: this.data.active.id,
        //                 userId: this.data.userInfo.id,
        //                 timerId: this.data.active.bookingTimes[i].id,
        //                 publisherId: this.data.active.creator,
        //                 amount: this.data.active.bookingTimes[i].amount
        //             }
        //             arr.push(param);
        //         }
        //     }
            // if (arr.length > 0) {
            //     network.postJson(api.activeOrderCreate, {
            //         activeId: this.data.active.id,
            //         publisherId: this.data.active.creator,
            //         userId: this.data.userInfo.id,
            //         amount: this.data.total,
            //         activePartList: arr,
            //         username: this.name,
            //         phone: this.data.mobile,
            //         status: 1
            //     }).then(res => {
            //         // if (this.data.total > 0) {
            //             // for (let i = 0; i < arr.length; i++) {
            //             //     arr[i].orderNo = res.data;
            //             // }
            //             // network.postJson(api.prepay, {
            //             //     description: res.data,
            //             //     outTradeNo: res.data,
            //             //     amount: this.data.total,
            //             //     openid: this.data.userInfo.openId,
            //             //     orderType: 1 // 1=活动，2=场地
            //             // }).then(data => {
            //                 // let result = data.data;
            //                 // wx.requestPayment({
            //                 //     timeStamp: result.timeStamp,
            //                 //     nonceStr: result.nonceStr,
            //                 //     package: result.packageVal,
            //                 //     signType: result.signType,
            //                 //     paySign: result.paySign,
            //                 //     success: res => {
            //                         // notice.postNotice("ActiveRefresh");
            //                         // notice.postNotice("GetGroupTimer");
                                    
            //                         // wx.reLaunch({
            //                         //     url: '/home/success/success?mode=ENROLL&activeId=' + this.data.active.id
            //                         // })
            //                 //     },
            //                 //     fail(res) {
            //                 //         console.log(res)
            //                 //     }
            //                 // })
            //             // })
            //         // }
            //     })
            // } else {
            //     wx.showToast({
            //         icon: 'none',
            //         title: '请选择活动场次'
            //     })
            // }
        // })
    },

    // onVisibleChange(e) {
    //     this.setData({
    //         visible: e.detail.visible,
    //     });
    // },

    backTap() {
        wx.navigateBack();
    }
})